package cn.bytecollege.admin.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Payment;
import java.util.List;
import cn.bytecollege.common.util.DBHelper;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

/**
 * @Auther：MR.P
 * @Date:2021/10/28 10 11
 * @Description:
 */
public class PaymentDao extends BaseDao<Payment,Integer> {
    private DBHelper helper = null;
    public PaymentDao(){
        helper = new DBHelper();
    }

    /**
     * 添加支付方式
     * @param payment
     * @return
     */
    public int save(Payment payment){
        String sql = "insert into payment (payment_name,delete_flag) values(?,?) ";
        Object [] objects = {payment.getPaymentName(),payment.getDeleteFlag()};
        return helper.executeUpdate(sql,objects);
    }

    /**
     *根据支付方式名字查询支付方式
     * @param paymentName
     * @return
     */
    public Payment findByPaymentName(String paymentName){
        String sql = "select payment_name from payment where payment_name = ? and delete_flag = 0 ";
        List<Payment> list = helper.executeQuery(sql,Payment.class,paymentName);
        Payment payment = null;
        if(list.size() > 0){
            payment = list.get(0);
        }
        return payment;
    }

    /**
     * 查询所有的支付方式
     * @return
     */
    public List<Payment> find(){
        String sql ="select payment_id,payment_name from payment where delete_flag = 0";
        return helper.executeQuery(sql,Payment.class);
    }

    /**
     * 根据支付方式的名字找出支付方式
     * @param paymentId
     * @return
     */
    public Payment findById(Integer paymentId){
        String sql = "select payment_id,payment_name from payment where payment_id = ? and delete_flag = 0 ";
        return helper.executeQuery(sql,Payment.class,paymentId).get(0);
    }

    /**
     * 修改支付方式
     * @param payment
     * @return
     */
    public int update(Payment payment){
        String sql = "update payment set payment_name = ? where payment_id = ?";
        return helper.executeUpdate(sql,payment.getPaymentName(),payment.getPaymentId());
    }
    /**
     * 添加商品时，商家可以下拉看有哪些商品支付方式，此方法就是查出商品支付方式。
     * 这方法不是重写的。
     * @return
     */
    public List<Payment> findPayment(){
        String sql = "select payment_id,payment_name from payment";
        return helper.executeQuery(sql,Payment.class);
    }

    /**
     * 根据支付方式id删除支付方式
     * @param paymentId
     * @return
     */
    public int deletePaymentById(Integer paymentId){
        String sql = "update payment set delete_flag=1 where payment_id = ? ";
        return helper.executeUpdate(sql,paymentId);
    }

}
